package com.yycx.module.bus.provider.handler.haiying.order;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.module.bus.client.entity.hy.HyOrderDetail;
import com.yycx.module.bus.client.enums.HySaleTypeEnum;
import com.yycx.module.bus.provider.service.BusOrderService;
import com.yycx.module.bus.provider.service.hy.HyOrderDetailService;
import liquibase.pro.packaged.B;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * 已审批详情商品明细处理器
 *
 * @author zyf
 */
@Component
public class HyBackOrderDetailsHandler implements PageInterceptor<HyOrderDetail> {


    @Autowired
    private BusOrderService orderService;
    @Autowired
    private HyOrderDetailService hyOrderDetailService;


    @Override
    public List<EntityMap> complete(CriteriaQuery<HyOrderDetail> cq, List<EntityMap> result, EntityMap extra) {
        for (EntityMap map : result) {
            Long orderDetailsId = map.getLong("orderDetailsId");
            BigDecimal saleNum = map.getBigDecimal("saleNum");
            //可退货数量
            BigDecimal surplusNum = saleNum;
            //检测是否可以退货
            List<HyOrderDetail> backOrderDetailsList = hyOrderDetailService.selectByParentId(orderDetailsId, HySaleTypeEnum.CHC);
            if (FlymeUtils.isNotEmpty(backOrderDetailsList)) {
                BigDecimal backNum = new BigDecimal("0");
                for (HyOrderDetail orderDetail : backOrderDetailsList) {
                    backNum=backNum.add(orderDetail.getSaleNum());
                }
                surplusNum = saleNum.add(backNum);
                map.put("backNum", backNum.abs());
            }
            map.put("surplusNum", surplusNum);
        }
        return result;
    }
}
